package Xadrez.View;

import Xadrez.Modelo.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.HashMap;
import javax.swing.ImageIcon;

/**
 *
 * @author daniel
 */
public class TabuleiroGrafico {

    boolean possibilidades[][];

    void DesenhaTabuleiro(Graphics g, int width, int height, Match board) {
        SetWindowSize(width, height);
        DesenhaTabuleiro(g, board);
    }

    public enum PlayState {

        CHOOSING_PIECE_CPU, CHOOSING_PIECE_PLAYER, CHOOSING_DESTINATION_CPU, CHOOSING_DESTINATION_PLAYER, MOVING_PIECE_CPU, MOVING_PIECE_PLAYER, MATE_CPU, MATE_PLAYER
    };
    private HashMap mapPieceToImage;
    public int CursorX;
    public int CursorY;
    public PlayState State;
    private int CurrentWindowHeight;
    private int CurrentWindowWidth;
    Square square1;
    Square square2;
    Move move;

    Point toGameSpace(Point point) {
        int _x = (int) ((point.x) * 8 / CurrentWindowWidth);
        int _y = (int) ((point.y) * 8 / CurrentWindowHeight);

        return new Point(_x, _y);
    }

    void SetPossibilidades(boolean[][] _possibilidades) {
// if (State == PlayState.CHOOSING_DESTINATION_PLAYER || State == PlayState.CHOOSING_DESTINATION_CPU)
        {
            this.possibilidades = _possibilidades.clone();
            System.out.println("possibilidades atualizadas");
        }
    }

    TabuleiroGrafico() {
        possibilidades = null;
        square1 = new Square();
        square2 = new Square();

        mapPieceToImage = new HashMap();
        CursorX = 0;
        CursorY = 0;

        mapPieceToImage.put("bispoBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/BispoP.png")).getImage());
        mapPieceToImage.put("bispoWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/BispoB.png")).getImage());
        mapPieceToImage.put("cavaloBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/CavaloP.png")).getImage());
        mapPieceToImage.put("cavaloWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/CavaloB.png")).getImage());
        mapPieceToImage.put("peãoBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/PeaoP.png")).getImage());
        mapPieceToImage.put("peãoWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/PeaoB.png")).getImage());
        mapPieceToImage.put("reiBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/ReiP.png")).getImage());
        mapPieceToImage.put("reiWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/ReiB.png")).getImage());
        mapPieceToImage.put("rainhaBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/DamaP.png")).getImage());
        mapPieceToImage.put("rainhaWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/DamaB.png")).getImage());
        mapPieceToImage.put("torreBlack", new ImageIcon(TabuleiroGrafico.class.getResource("images/TorreP.png")).getImage());
        mapPieceToImage.put("torreWhite", new ImageIcon(TabuleiroGrafico.class.getResource("images/TorreB.png")).getImage());
        State = PlayState.CHOOSING_PIECE_PLAYER;

    }

    public void SetWindowSize(int x, int y) {
        CurrentWindowWidth = 8 * 45;
        CurrentWindowHeight =8 * 45;
    }

    public void Click(int x, int y, Match board) {
        int _x = x;
        int _y = y;
                    CursorX = _x;
                    CursorY = _y;
        switch (State) {
            case CHOOSING_PIECE_PLAYER: {
                possibilidades = null;
                if (board.getPiece(_x, _y) != null && board.getPiece(_x, _y).getColor().equals(board.getTurn())) {
                    CursorX = _x;
                    CursorY = _y;
                    State = PlayState.CHOOSING_DESTINATION_PLAYER;
                    System.out.println("choosing destination");
                }
                return;
            }
            case CHOOSING_DESTINATION_PLAYER: {
                System.out.println("considerando possibilidades...");
                ///caso o jogador desista e clique em outra peça
/*
                if (board.getPiece(_x, _x) != null && board.getPiece(CursorX, CursorY) != null && board.getPiece(_x, _y).getColor().equalsIgnoreCase(board.getPiece(CursorX, CursorY).getColor()))
                {
                    CursorX = _x;
                    CursorY = _y;
                    System.out.println("still choosing destination");
                    return;
                }
*/

            if (possibilidades!=null && possibilidades[_x][_y])
                {
                    State = PlayState.CHOOSING_PIECE_PLAYER;
                    System.out.println("moving piece");
                    CursorX = _x;
                    CursorY = _y;
                    possibilidades=null;
                }
            }
            break;


            default:
                break;

        }

        //   System.out.println(String.valueOf(State) + ":" + String.valueOf(CursorX) + "," + String.valueOf(CursorY));
    }

    public void DesenhaTabuleiro(Graphics g, Match board) {
        System.out.println("refresh gráfico");
        float ratiox = (float) (CurrentWindowWidth / 8.0);
        float ratioy = (float) (CurrentWindowHeight / 8.0);
        Color cor = Color.RED;
        Image img;
        g.setColor(Color.black);
        Piece piece;
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {


                if (((x + y) % 2) == 1) {
                    cor = (Color.BLACK);
                } else {
                    cor = (Color.WHITE);
                }



                if (!(CursorX == x && CursorY == y))
                {
                    if (possibilidades != null && possibilidades[x][y]) {
                        cor = Color.GREEN;
                    }
                }

                g.setColor(cor);
                g.fillRect((int) (x * ratiox), (int) (y * ratioy), (int) (ratiox), (int) (ratioy));




                piece = board.getPiece(x, y);
                if (piece != null) {
                    img = (Image) mapPieceToImage.get(piece.toString() + piece.getColor());
                  //  g.drawImage(img, (int) (x * ratiox), (int) (y * ratioy), (int) ((x + 1) * ratiox), (int) ((y + 1) * ratioy), 0, 0, img.getWidth(null), img.getHeight(null), cor, null);
                    g.drawImage(img,(int) (x * ratiox), (int) (y * ratioy), cor, null);
                }
                if (CursorX == x && CursorY == y)
                {
                    g.setColor(Color.RED);
                    for (int e=0;e<3;e++)
                    g.drawRect((int) (x * ratiox)+e, (int) (y * ratioy)+e, (int) (ratiox)-2*e, (int) (ratioy)-2*e);

                }

            }
        }



    }
}
